'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const {formatDecimal} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, BIGINT, TINYINT, DECIMAL } = app.Sequelize;
    const BalanceRecord = app.model.define(
        'BalanceRecord',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            out_bill_no: STRING,
            transfer_bill_no: STRING,
            fail_reason: STRING,
            order_id: BIGINT(20),
            order_item_id: BIGINT(20),
            order_code_id: BIGINT(20),
            product_name: {
                type: STRING(255),
                allowNull: true,
                comment: '商品名称'
            },
            user_id: {
                type: INTEGER,
                allowNull: false,
                comment: '用户ID'
            },
            user_type: {
                type: INTEGER,
                allowNull: false,
                comment: '1 普通用户 2 商家 3 一级分销商 4 二级分销商'
            },
            amount: {
                type: DECIMAL(10, 2),
                allowNull: false,
                comment: '金额，正数表示收入，负数表示提现',
                get() {
                    return formatDecimal(this.getDataValue('amount'))
                }
            },
            type: {
                type: TINYINT,
                allowNull: false,
                comment: '1 提现 2 分销收入 3 订单扣减 4 订单退款'
            },
            description: {
                type: STRING(255),
                allowNull: true,
                comment: '描述'
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'balance_record',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    BalanceRecord.associate = function () {
        app.model.Business.BalanceRecord.belongsTo(app.model.Business.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user'
        });
        // order_code
        app.model.Business.BalanceRecord.belongsTo(app.model.Business.OrderCode, {
            foreignKey: 'order_code_id',
            targetKey: 'id',
            as: 'orderCode'
        })
    };
    return BalanceRecord
};
